package com.example.workflow.util;


import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
import jakarta.mail.*;
import jakarta.mail.internet.*;

import jakarta.activation.FileDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.Properties;

//public class MailUtils {
//
//    private static final String fromEmail = "123@qq.com";
//    private static final String toEmail = "123@qq.com";
//
//    // 私有构造函数，防止实例化
//    private MailUtils() {
//        throw new UnsupportedOperationException("Utility class");
//    }
//
//    // 获取邮件会话
//    private static Session getSession(String host, String port, final String username, final String password) {
//        Properties props = new Properties();
//        props.put("mail.smtp.auth", "true");
//        props.put("mail.smtp.starttls.enable", "true");
//        props.put("mail.smtp.host", host);
//        props.put("mail.smtp.port", port);
//
//        return Session.getInstance(props, new Authenticator() {
//            protected PasswordAuthentication getPasswordAuthentication() {
//                return new PasswordAuthentication(username, password);
//            }
//        });
//    }
//
//    /**
//     * 发送文本文件
//     *
//     * @param host
//     * @param port
//     * @param username
//     * @param password
//     * @param toAddress
//     * @param subject
//     * @param message
//     * @throws MessagingException
//     */
//    public static void sendTextEmail(String host, String port, String username, String password,
//                                     String toAddress, String subject, String message) throws MessagingException {
//        Session session = getSession(host, port, username, password);
//        Message msg = new MimeMessage(session);
//
//        msg.setFrom(new InternetAddress(username));
//        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
//        msg.setSubject(subject);
//        msg.setText(message);
//
//        Transport.send(msg);
//    }
//
//    /**
//     * 发送html邮件
//     *
//     * @param host
//     * @param port
//     * @param username
//     * @param password
//     * @param toAddress
//     * @param subject
//     * @param htmlContent
//     * @throws MessagingException
//     */
//    public static void sendHtmlEmail(String host, String port, String username, String password,
//                                     String toAddress, String subject, String htmlContent) throws MessagingException {
//        Session session = getSession(host, port, username, password);
//        Message msg = new MimeMessage(session);
//
//        msg.setFrom(new InternetAddress(username));
//        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
//        msg.setSubject(subject);
//        // 设置 HTML 内容
//        msg.setContent(htmlContent, "text/html; charset=utf-8");
//        Transport.send(msg);
//    }
//
//
//    // 发送带附件的邮件
//    public static void sendEmailWithAttachment(String host, String port, String username, String password,
//                                               String toAddress, String subject, String message, File attachment) throws MessagingException {
//        Session session = getSession(host, port, username, password);
//        Message msg = new MimeMessage(session);
//        msg.setFrom(new InternetAddress(username));
//        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
//        msg.setSubject(subject);
//
//        // 创建消息部分
//        MimeBodyPart messageBodyPart = new MimeBodyPart();
//        messageBodyPart.setText(message);
//
//        // 创建附件部分
//        MimeBodyPart attachPart = new MimeBodyPart();
//        DataSource source = new FileDataSource(attachment);
//        attachPart.setDataHandler(new DataHandler(source));
//        attachPart.setFileName(attachment.getName());
//
//        // 创建多部分内容
//        Multipart multipart = new MimeMultipart();
//        multipart.addBodyPart(messageBodyPart);
//        multipart.addBodyPart(attachPart);
//
//        // 设置邮件内容
//        msg.setContent(multipart);
//
//        Transport.send(msg);
//    }
//
//}

@Component
public class MailUtils {
    // 注入邮件发送对象
    @Autowired
    private JavaMailSender mailSender;

    private final String fromEmail = "rainrop_rainbow@163.com";

    /**
     * 简单邮件发送
     *
     * @param toEmail 收件人邮箱
     * @param copyEmail 抄送人邮箱
     * @param secretEmail 密送人邮箱
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendMail(String toEmail,String copyEmail,String secretEmail, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail); // 设置发件人邮箱
        message.setTo(toEmail); // 设置收件人邮箱
        if (copyEmail != null) {
            message.setCc(copyEmail); // 设置抄送人邮箱
        }
        if (secretEmail != null) {
            message.setBcc(secretEmail); // 设置密送人邮箱
        }
        message.setSubject(subject); // 设置邮件主题
        message.setText(content); // 设置文本内容
        message.setSentDate(new Date()); // 设置邮件发送时间
        // 发送
        mailSender.send(message);
    }
}

